﻿using System;
using System.Collections.Generic;
using System.Security.Claims;
using DataBaseConnect.App;
using Entity;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Utils;

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class FormController : BaseController
    {
        #region 构造实例化

        public IFormAppService _formAppService;

        public FormController(IFormAppService formAppService)
        {
            _formAppService = formAppService;
        }

        #endregion 

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 编辑页
        /// </summary>
        /// <returns></returns>
        public IActionResult EditForm()
        {
            return View();
        }

        /// <summary>
        /// 添加页
        /// </summary>
        /// <returns></returns>
        public IActionResult AddForm()
        {
            return View();
        }

        /// <summary>
        /// 查看页
        /// </summary>
        /// <returns></returns>
        public IActionResult SeeForm()
        {
            return View();
        }

        /// <summary>
        /// 测试页
        /// </summary>
        /// <returns></returns>
        public IActionResult test()
        {
            return View();
        }
        #endregion

        #region 功能实现区

        /// <summary>
        /// 添加表单信息
        /// </summary>
        /// <param name="Input">参数实体</param>
        /// <returns></returns>
        public JsonResult AddFormMsg(Form Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                Input.CreateTime = DateTime.Now;
                //Input.Id = 1;
                Input.CreateUser = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
                var result = _formAppService.AddFromMsg(Input);
                receiveStatus.msg = result.msg;
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "新增失败：" + ex.Message;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 获取说有的表单和根据表单名称查询
        /// </summary>
        /// <param name="Name">表单名称</param>
        /// <returns></returns>
        public JsonResult GetFormList(string Name)
        {
            ReceiveStatus<Form> receiveStatus = new ReceiveStatus<Form>();
            List<Form> list = new List<Form>();
            try
            {
                //得到所有菜单
                list = _formAppService.GetFormList(Name);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 根据主键获取表单
        /// </summary>
        /// <param name="Id">id</param>
        /// <returns></returns>
        public string GetFormId(int Id)
        {
            ReceiveStatus<Form> receiveStatus = new ReceiveStatus<Form>();
            try
            {
                var form = _formAppService.GetFormId(Id);
                receiveStatus.msg = "成功";
                receiveStatus.data = form;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return JsonConvert.SerializeObject(receiveStatus, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 编辑表单信息
        /// </summary>
        /// <param name="Input">参数实体</param>
        /// <returns></returns>
        public JsonResult EditFormMsg(Form Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                Input.UpdateTime = DateTime.Now;
                Input.UpdateUser = Convert.ToInt32(User.FindFirst(ClaimTypes.Sid).Value);
                var result = _formAppService.EditFormMsg(Input);
                receiveStatus.msg = result.msg;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }
        #endregion

    }
}